#include "stdafx.h"
#include "ctl_picture.h"
#include "config.h"
#include "window_project.h"


picture::picture(data_xml* aBelongTo)
: control(aBelongTo)
, m_image(NULL)
{

}

void picture::set_image_file(wstring aStrFileName)
{
	m_strImageString = aStrFileName;

	if (m_image)
	{
		delete m_image;
	}

	int spPos = aStrFileName.find_first_of(L'|');

	m_strImgFileName = aStrFileName;
	wstring rectStr  = aStrFileName;
	if (wstring::npos != spPos)
	{
		m_strImgFileName = m_strImgFileName.erase(spPos);
		rectStr = rectStr.erase(0, spPos + 1);	
		load_helper::parse_rect(rectStr, m_imageRect);
	}

	wstring strTmpPath;
	if (m_belongTo)
	{
		strTmpPath = path_helper::process_file(m_strImgFileName, m_belongTo->get_file_name());
	}else
	{
		strTmpPath = path_helper::process_file(m_strImgFileName);
	}
	 
	m_image = new Gdiplus::Image(strTmpPath.c_str(), FALSE);
	m_strImgFileName = strTmpPath;
}

void picture::add_attribute(wstring aStrName, wstring aStrValue)
{
	if (aStrName.compare(L"fill_mode") == 0)
	{
		if (!load_helper::parse_fill_mode(aStrValue, m_fillMode))
		{
			return;
		}
		return;
	}

	if (aStrName.compare(L"image") == 0)
	{
		set_image_file(aStrValue);
		return;
	}
	

	__super::add_attribute(aStrName, aStrValue);
}


void picture::on_clone_attributes(control* aSrc)
{
	__super::on_clone_attributes(aSrc);

	picture * pSrc = (picture *)aSrc;
	m_fillMode		= pSrc->m_fillMode;
	m_fillCenter	= pSrc->m_fillCenter;
	m_fillRepeat	= pSrc->m_fillRepeat;
	m_strImageString= pSrc->m_strImageString;
	m_strImgFileName= pSrc->m_strImgFileName;
	m_imageRect		= pSrc->m_imageRect;	
	m_image			= NULL;
	set_image_file(m_strImageString);
}

bool picture::load(IXMLDOMNode* aXmlNode)
{
	if (!__super::load(aXmlNode))
	{
		return false;
	}

	return true;
}

bool picture::save(IXMLDOMNode* aXmlNode)
{
	IXMLDOMElement*		pXmlElement = (IXMLDOMElement*) aXmlNode;
	if (m_strImageString.length() > 0 && 
		(!m_pReference || !m_pReference->exist_property(L"image") || 
		m_pReference->get_property(L"image") != m_strImageString))
	{
		pXmlElement->setAttribute(L"image",		(_variant_t)m_strImageString.c_str());
	}
	return true;
}

void picture::render(Graphics & aGraphics)
{
	point p = get_absolute_pos();
	if (m_image)
	{
		//aGraphics.DrawImage(m_image, (float)p.x, (float)p.y, x, y, w, h, UnitPixel);
		if (m_imageRect.width() > 0 && m_imageRect.height() > 0)
		{
			aGraphics.DrawImage(m_image, get_absolute_rect(), 
				(float)m_imageRect.left, (float)m_imageRect.top, 
				(float)m_imageRect.width(), (float)m_imageRect.height(), UnitPixel);
		}else
			aGraphics.DrawImage(m_image, (Rect)get_absolute_rect());
		
	}
}


void picture::build_properties()
{
	__super::build_properties();

	CMFCPropertyGridProperty* group = NULL;
	CMFCPropertyGridProperty* property = NULL;
	group = m_Properties.alloc_group(this, L"picture");

	property = m_Properties.alloc_property_normal(this, L"image", (_variant_t)m_strImgFileName.c_str());
	property->SetValue((_variant_t)(wchar_t*)m_strImageString.c_str());
	group->AddSubItem(property);

	m_Properties.add_property(group);
}

s_properties& picture::get_properties()
{
	__super::get_properties();

	CMFCPropertyGridProperty* prop = NULL;

	prop = m_Properties.find_by_name(L"image");
	if (prop)
	{
		prop->SetValue((_variant_t)m_strImageString.c_str());
	}

	return m_Properties;
}

void picture::on_property_changed(CMFCPropertyGridProperty* aProperty)
{
	wstring strOldValue;
	wstring strNewValue;

	const wchar_t* strName = aProperty->GetName();
	if (wcscmp(strName, L"image") == 0)
	{
		strOldValue = m_strImageString;
		set_image_file(aProperty->GetValue().bstrVal);
		strNewValue = m_strImageString;
	}else
	{
		__super::on_property_changed(aProperty);
		return;
	}

	op_modify_ctl* op = new op_modify_ctl(this, wstring((wchar_t*)strName), strOldValue, strNewValue);
	if (m_belongTo)
	{
		m_belongTo->m_undoRedo.operation(op);
	}else
		delete op;

	if (m_belongTo)
	{
		m_belongTo->refrash();
	}
}